package de.mhu.mandalaui;

import de.mhu.mandalaui.actions.Action;
import de.mhu.mandalaui.attributes.Attribute;
import de.mhu.mandalaui.attributes.BooleanAttribute;
import de.mhu.mandalaui.attributes.MultiAttribute;
import de.mhu.mandalaui.bind.Element;

public abstract class Component implements Styleable, Attachable {
	
	private Component parent;
	private String classes;
	private String extraStyle;
	private String id;
	private Action clickAction;
	private Element bindValue;
	private BooleanAttribute bindVisible;
	private MultiAttribute bindCss;
	private Window window;
	private Attribute bindWith;
	private EMPHASIS emphasisStyle;
	private SIZE sizeStyle;

	public synchronized void componentAttach(Object parent) {
		if (this.parent != null || parent == null) return; // TODO log message
		this.parent = (Component) parent;
		this.window = this.parent.getWindow();
		if (clickAction != null) clickAction.setOwner(this);
		if (bindValue != null) bindValue.setOwner(this);
		if (bindCss != null) bindCss.setOwner(this);
		if (bindVisible != null) bindVisible.setOwner(this);

		doInitialize();
	}
	
	public synchronized void componentDetach() {
		if (this.parent == null) return;
		doDestroy();
		this.parent = null;
	}
	
	public Component getParent() {
		return parent;
	}
	
	protected abstract void doInitialize();
	protected abstract void doDestroy();
	
	public abstract void render(Response response);
	
	public void setStyleClass(String classes) {
		this.classes = classes;
	}
	
	public void setExtraStyle(String style) {
		this.extraStyle = style;
	}

	@Override
	public String getStyleClass() {
		return classes;
	}

	@Override
	public String getExtraStyle() {
		return extraStyle;
	}

	public String getId() {
		if (id == null) id = getWindow().nextId();
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Action getClickAction() {
		return clickAction;
	}

	public void setClickAction(Action clickAction) {
		if (clickAction != null) clickAction.setOwner(this);
		this.clickAction = clickAction;
	}

	public Element getBindValue() {
		return bindValue;
	}

	public void setBindValue(Element bindValue) {
		this.bindValue = bindValue;
	}

	public BooleanAttribute getBindVisible() {
		return bindVisible;
	}

	public void setBindVisible(BooleanAttribute bindVisible) {
		this.bindVisible = bindVisible;
	}

	public MultiAttribute getBindCss() {
		return bindCss;
	}

	public void setBindCss(MultiAttribute bindCss) {
		this.bindCss = bindCss;
	}

	public Window getWindow() {
		if (window == null && parent != null) {
			window = parent.getWindow();
		}
		return window;
	}

	public Attribute getBindWith() {
		return bindWith;
	}

	public void setBindWith(Attribute bindWith) {
		this.bindWith = bindWith;
	}

	public EMPHASIS getEmphasisStyle() {
		return emphasisStyle;
	}

	public void setEmphasisStyle(EMPHASIS emphasisStyle) {
		this.emphasisStyle = emphasisStyle;
	}
	
	public String getStyleBase() {
		return null;
	}

	public SIZE getSizeStyle() {
		return sizeStyle;
	}

	public void setSizeStyle(SIZE sizeStyle) {
		this.sizeStyle = sizeStyle;
	}

}
